{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "81VDUp0E-ImU"
      },
      "source": [
        "#**RAG Fusion - The New Star of Search Technology**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "Q0OqC7ShJ9Zk"
      },
      "outputs": [],
      "source": [
        "# Installing the required dependencies\n",
        "!pip install \"langchain==0.0.344\" \"openai==0.28\" pypdf tiktoken -q"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "UifWm31durQd"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "WoWutL3GKEDi"
      },
      "outputs": [],
      "source": [
        "# Importing Required Dependencies\n",
        "import openai\n",
        "from langchain.llms import OpenAI\n",
        "from langchain.embeddings import OpenAIEmbeddings\n",
        "from langchain_community.vectorstores import LanceDB\n",
        "import tiktoken"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1Tu4KwOwsVGJ",
        "outputId": "080b8357-ca16-4a05-c6d0-6439bfbec814"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2024-09-12 10:15:37--  https://ncert.nic.in/ncerts/l/leph202.pdf\n",
            "Resolving ncert.nic.in (ncert.nic.in)... 164.100.166.133\n",
            "Connecting to ncert.nic.in (ncert.nic.in)|164.100.166.133|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 3530023 (3.4M) [application/pdf]\n",
            "Saving to: ‘doc.pdf’\n",
            "\n",
            "doc.pdf             100%[===================>]   3.37M   453KB/s    in 8.9s    \n",
            "\n",
            "2024-09-12 10:15:47 (389 KB/s) - ‘doc.pdf’ saved [3530023/3530023]\n",
            "\n"
          ]
        }
      ],
      "source": [
        "# Downloading the files\n",
        "!wget https://ncert.nic.in/ncerts/l/leph202.pdf -O doc.pdf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cX2lxQew_Oy7"
      },
      "source": [
        "Splitting our documents into chunks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "HeGxztMcukx8"
      },
      "outputs": [],
      "source": [
        "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
        "from langchain.document_loaders import PyPDFLoader\n",
        "\n",
        "# Load the  pdf\n",
        "pdf_folder_path = \"/content/doc.pdf\"\n",
        "\n",
        "loader = PyPDFLoader(pdf_folder_path)\n",
        "docs = loader.load_and_split()\n",
        "\n",
        "text_splitter = RecursiveCharacterTextSplitter(\n",
        "    chunk_size=500,\n",
        "    chunk_overlap=50,\n",
        ")\n",
        "documents = text_splitter.split_documents(docs)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "QF5fjxQYzEYN"
      },
      "outputs": [],
      "source": [
        "embeddings = OpenAIEmbeddings()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eTk-rbff_brP"
      },
      "source": [
        "###Using **LANCEDB** vector store for store and retreive embeddings."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "9WL6efEUv1wV"
      },
      "outputs": [],
      "source": [
        "from langchain.vectorstores import LanceDB\n",
        "import lancedb\n",
        "\n",
        "# lancedb as vectorstore\n",
        "db = lancedb.connect(\"/tmp/lancedb\")\n",
        "table = db.create_table(\n",
        "    \"documents\",\n",
        "    data=[\n",
        "        {\n",
        "            \"vector\": embeddings.embed_query(\"Hello World\"),\n",
        "            \"text\": \"Hello World\",\n",
        "            \"id\": \"1\",\n",
        "        }\n",
        "    ],\n",
        "    mode=\"overwrite\",\n",
        ")\n",
        "vector_store = LanceDB.from_documents(documents, embeddings, connection=table)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yUCb44G7AAV7"
      },
      "source": [
        "Generating different queries relevant to the original query given by user."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "bA03C3rZbLxI"
      },
      "outputs": [],
      "source": [
        "def generate_queries_chatgpt(original_query):\n",
        "    response = openai.ChatCompletion.create(\n",
        "        model=\"gpt-3.5-turbo\",\n",
        "        messages=[\n",
        "            {\n",
        "                \"role\": \"system\",\n",
        "                \"content\": \"You are a helpful assistant that generates multiple search queries based on a single input query.\",\n",
        "            },\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": f\"Generate multiple search queries related to: {original_query}\",\n",
        "            },\n",
        "            {\"role\": \"user\", \"content\": \"OUTPUT (4 queries):\"},\n",
        "        ],\n",
        "    )\n",
        "    generated_queries = response.choices[0].message.content.strip().split(\"\\n\")\n",
        "    return generated_queries"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6GpXiCqgAZKr"
      },
      "source": [
        "Search relevant documents related to query in vector store."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "ozMgE8U41Kej"
      },
      "outputs": [],
      "source": [
        "def vector_search(query):\n",
        "    search_results = {}\n",
        "    retrieved_docs = vector_store.similarity_search(query)\n",
        "    for i in retrieved_docs:\n",
        "        search_results[i.page_content] = i.metadata[\"_distance\"]\n",
        "    search_results = {\n",
        "        doc: score for doc, score in sorted(search_results.items(), key=lambda x: x[1])\n",
        "    }\n",
        "    return search_results"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GQrEWBBvAkeV"
      },
      "source": [
        "##Major Component of the RAG Fusion - **Reciprocal Rank Fusion Algorithm**\n",
        ">This algorithm ranks documents on the basis of similarity to the query."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "Km_gu8jDBKoz"
      },
      "outputs": [],
      "source": [
        "def reciprocal_rank_fusion(\n",
        "    search_results_dict, k=60\n",
        "):  # k=60 taken for optimum results according to paper.\n",
        "    fused_scores = {}\n",
        "    print(\"Initial individual search result ranks:\")\n",
        "    for query, doc_scores in search_results_dict.items():\n",
        "        print(f\"For query '{query}': {doc_scores}\")\n",
        "\n",
        "    for query, doc_scores in search_results_dict.items():\n",
        "        for rank, (doc, score) in enumerate(\n",
        "            sorted(doc_scores.items(), key=lambda x: x[1])\n",
        "        ):\n",
        "            if doc not in fused_scores:\n",
        "                fused_scores[doc] = 0\n",
        "            previous_score = fused_scores[doc]\n",
        "            fused_scores[doc] += 1 / (rank + k)\n",
        "            print(\n",
        "                f\"Updating score for {doc} from {previous_score} to {fused_scores[doc]} based on rank {rank} in query '{query}'\"\n",
        "            )\n",
        "\n",
        "    reranked_results = {\n",
        "        doc: score\n",
        "        for doc, score in sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)\n",
        "    }\n",
        "    print(\"Final reranked results:\", reranked_results)\n",
        "    return reranked_results"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "P-DC5M6sBJrH"
      },
      "source": [
        "Generating output based on the reranked documents."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "ijlZeMy0E76a"
      },
      "outputs": [],
      "source": [
        "def generate_output(original_query, reranked_results):\n",
        "    reranked_docs = [i for i in reranked_results.keys()]\n",
        "    context = \"\\n\".join(reranked_docs)\n",
        "    response = openai.ChatCompletion.create(\n",
        "        model=\"gpt-3.5-turbo\",\n",
        "        messages=[\n",
        "            {\n",
        "                \"role\": \"system\",\n",
        "                \"content\": \"You are a helpful assistant that answers user's questions based on the context provided.\\nDo not make up an answer if you do not know it, stay within the bounds of the context provided, if you don't know the answer, say that you don't have enough information on the topic!\",\n",
        "            },\n",
        "            {\"role\": \"user\", \"content\": f\"CONTEXT: {context}\\nQUERY: {original_query}\"},\n",
        "            {\"role\": \"user\", \"content\": \"ANSWER:\"},\n",
        "        ],\n",
        "    )\n",
        "\n",
        "    response = response.choices[0].message.content.strip()\n",
        "    return response"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "p3tb2qdsBmqt"
      },
      "source": [
        "Now on to the final generation part with respect to queries given by user."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "_yg7OK7aBS-n"
      },
      "outputs": [],
      "source": [
        "original_query = \"Huygens Principle\"\n",
        "generated_queries = generate_queries_chatgpt(original_query)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6C2b4yafDavx",
        "outputId": "986a87ae-2f99-47f5-9911-94b926bb67c7"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['1. Huygens principle explanation',\n",
              " '2. Applications of Huygens principle',\n",
              " '3. Wave propagation according to Huygens principle',\n",
              " '4. Huygens-Fresnel principle comparison']"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ],
      "source": [
        "generated_queries"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "BKpja3KMDaop"
      },
      "outputs": [],
      "source": [
        "# Vector Search and document retreival for all the generated queries.\n",
        "all_results = {}\n",
        "for query in generated_queries:\n",
        "    search_results = vector_search(query)\n",
        "    all_results[query] = search_results"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EQYwX2ZcEH3G",
        "outputId": "74336f39-5a2c-4a9a-efdf-47cb6a86ff28"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Initial individual search result ranks:\n",
            "For query '1. Huygens principle explanation': {'Physics\\n354small portion of the sphere can be considered as a plane and we have\\nwhat is known as a plane wave  [Fig. 10.1(b)].\\nNow, if we know the shape of the wavefront at t = 0, then Huygens\\nprinciple allows us to determine the shape of the wavefront at a later\\ntime τ. Thus, Huygens principle is essentially a geometrical construction,\\nwhich given the shape of the wafefront at any time allows us to determine\\nthe shape of the wavefront at a later time. Let us consider a diverging': 0.2861475646495819, 'In this chapter we will first discuss the original formulation of the\\nHuygens principle  and derive the laws of reflection and refraction. In\\nSections 10.4 and 10.5 , we will discuss the phenomenon of interference\\nwhich is based on the principle of superposition. In Sec tion 10.6 we\\nwill discuss the phenomenon of diffraction which is based on Huygens-\\nFresnel principle. Finally in Sec tion 10.7 we will discuss the\\nphenomenon of polari sation which is based on the fact that the light': 0.29485398530960083, 'in Chapter 9, can also be understood on the basis of W ave Optics. Then\\nwe described Y oung’s double slit experiment which was a tur ning point\\nin the study of optics. Finally, we described some associated points such\\nas diffraction, resolution, polarisation, and validity of ray optics. In the\\nnext chapter , you will see how new experiments led to new theories at\\nthe turn of the century around 1900 A.D.\\nSUMMARY\\n1.Huygens’ principle tells us that each point on a wavefront is a source': 0.29967761039733887, 'consistent with experiment?\\n10.13 You have lear nt in the text how Huygens’ principle leads to the laws\\nof reflection and refraction. Use the same principle to deduce directly\\nthat a point object placed in front of a plane mirror produces a\\nvirtual image whose distance from the mirror is equal to the object\\ndistance fr om the mirr or.\\n10.14 Let us list some of the factors, which could possibly influence the\\nspeed of wave propagation:\\n(i)nature of the source.\\n(ii)direction of propagation.': 0.3166719377040863}\n",
            "For query '2. Applications of Huygens principle': {'In this chapter we will first discuss the original formulation of the\\nHuygens principle  and derive the laws of reflection and refraction. In\\nSections 10.4 and 10.5 , we will discuss the phenomenon of interference\\nwhich is based on the principle of superposition. In Sec tion 10.6 we\\nwill discuss the phenomenon of diffraction which is based on Huygens-\\nFresnel principle. Finally in Sec tion 10.7 we will discuss the\\nphenomenon of polari sation which is based on the fact that the light': 0.31198158860206604, 'Physics\\n354small portion of the sphere can be considered as a plane and we have\\nwhat is known as a plane wave  [Fig. 10.1(b)].\\nNow, if we know the shape of the wavefront at t = 0, then Huygens\\nprinciple allows us to determine the shape of the wavefront at a later\\ntime τ. Thus, Huygens principle is essentially a geometrical construction,\\nwhich given the shape of the wafefront at any time allows us to determine\\nthe shape of the wavefront at a later time. Let us consider a diverging': 0.31660452485084534, 'consistent with experiment?\\n10.13 You have lear nt in the text how Huygens’ principle leads to the laws\\nof reflection and refraction. Use the same principle to deduce directly\\nthat a point object placed in front of a plane mirror produces a\\nvirtual image whose distance from the mirror is equal to the object\\ndistance fr om the mirr or.\\n10.14 Let us list some of the factors, which could possibly influence the\\nspeed of wave propagation:\\n(i)nature of the source.\\n(ii)direction of propagation.': 0.3187037706375122, 'in Chapter 9, can also be understood on the basis of W ave Optics. Then\\nwe described Y oung’s double slit experiment which was a tur ning point\\nin the study of optics. Finally, we described some associated points such\\nas diffraction, resolution, polarisation, and validity of ray optics. In the\\nnext chapter , you will see how new experiments led to new theories at\\nthe turn of the century around 1900 A.D.\\nSUMMARY\\n1.Huygens’ principle tells us that each point on a wavefront is a source': 0.3304024338722229}\n",
            "For query '3. Wave propagation according to Huygens principle': {'Physics\\n354small portion of the sphere can be considered as a plane and we have\\nwhat is known as a plane wave  [Fig. 10.1(b)].\\nNow, if we know the shape of the wavefront at t = 0, then Huygens\\nprinciple allows us to determine the shape of the wavefront at a later\\ntime τ. Thus, Huygens principle is essentially a geometrical construction,\\nwhich given the shape of the wafefront at any time allows us to determine\\nthe shape of the wavefront at a later time. Let us consider a diverging': 0.24890434741973877, 'consistent with experiment?\\n10.13 You have lear nt in the text how Huygens’ principle leads to the laws\\nof reflection and refraction. Use the same principle to deduce directly\\nthat a point object placed in front of a plane mirror produces a\\nvirtual image whose distance from the mirror is equal to the object\\ndistance fr om the mirr or.\\n10.14 Let us list some of the factors, which could possibly influence the\\nspeed of wave propagation:\\n(i)nature of the source.\\n(ii)direction of propagation.': 0.2550153136253357, '355Wave Optics\\n10.3 REFRACTION  AND REFLECTION  OF\\nPLANE WAVES USING  HUYGENS\\nPRINCIPLE\\n10.3.1  Refraction of a plane wave\\nWe will now use Huygens principle to derive the laws of\\nrefraction. Let PP ′ represent the surface separating medium\\n1 and medium 2, as shown in Fig. 10.4. Let v1 and v2\\nrepresent the speed of light in medium 1 and medium 2,\\nrespectively. W e assume a plane wavefr ont AB pr opagating\\nin the direction A ′A incident on the interface at an angle i': 0.267469584941864, '353Wave Optics\\n10.2  H UYGENS  PRINCIPLE\\nWe would first define a wavefr ont: when we dr op a small stone on a calm\\npool of water , waves spr ead out fr om the point of impact. Every point on\\nthe surface starts oscillating with time. At any instant, a photograph of\\nthe surface would show circular rings on which the disturbance is\\nmaximum. Clearly, all points on such a circle are oscillating in phase\\nbecause they are at the same distance from the source. Such a locus of': 0.2760622501373291}\n",
            "For query '4. Huygens-Fresnel principle comparison': {'consistent with experiment?\\n10.13 You have lear nt in the text how Huygens’ principle leads to the laws\\nof reflection and refraction. Use the same principle to deduce directly\\nthat a point object placed in front of a plane mirror produces a\\nvirtual image whose distance from the mirror is equal to the object\\ndistance fr om the mirr or.\\n10.14 Let us list some of the factors, which could possibly influence the\\nspeed of wave propagation:\\n(i)nature of the source.\\n(ii)direction of propagation.': 0.29138022661209106, 'In this chapter we will first discuss the original formulation of the\\nHuygens principle  and derive the laws of reflection and refraction. In\\nSections 10.4 and 10.5 , we will discuss the phenomenon of interference\\nwhich is based on the principle of superposition. In Sec tion 10.6 we\\nwill discuss the phenomenon of diffraction which is based on Huygens-\\nFresnel principle. Finally in Sec tion 10.7 we will discuss the\\nphenomenon of polari sation which is based on the fact that the light': 0.30527716875076294, 'in Chapter 9, can also be understood on the basis of W ave Optics. Then\\nwe described Y oung’s double slit experiment which was a tur ning point\\nin the study of optics. Finally, we described some associated points such\\nas diffraction, resolution, polarisation, and validity of ray optics. In the\\nnext chapter , you will see how new experiments led to new theories at\\nthe turn of the century around 1900 A.D.\\nSUMMARY\\n1.Huygens’ principle tells us that each point on a wavefront is a source': 0.32674726843833923, '355Wave Optics\\n10.3 REFRACTION  AND REFLECTION  OF\\nPLANE WAVES USING  HUYGENS\\nPRINCIPLE\\n10.3.1  Refraction of a plane wave\\nWe will now use Huygens principle to derive the laws of\\nrefraction. Let PP ′ represent the surface separating medium\\n1 and medium 2, as shown in Fig. 10.4. Let v1 and v2\\nrepresent the speed of light in medium 1 and medium 2,\\nrespectively. W e assume a plane wavefr ont AB pr opagating\\nin the direction A ′A incident on the interface at an angle i': 0.3437243103981018}\n",
            "Updating score for Physics\n",
            "354small portion of the sphere can be considered as a plane and we have\n",
            "what is known as a plane wave  [Fig. 10.1(b)].\n",
            "Now, if we know the shape of the wavefront at t = 0, then Huygens\n",
            "principle allows us to determine the shape of the wavefront at a later\n",
            "time τ. Thus, Huygens principle is essentially a geometrical construction,\n",
            "which given the shape of the wafefront at any time allows us to determine\n",
            "the shape of the wavefront at a later time. Let us consider a diverging from 0 to 0.016666666666666666 based on rank 0 in query '1. Huygens principle explanation'\n",
            "Updating score for In this chapter we will first discuss the original formulation of the\n",
            "Huygens principle  and derive the laws of reflection and refraction. In\n",
            "Sections 10.4 and 10.5 , we will discuss the phenomenon of interference\n",
            "which is based on the principle of superposition. In Sec tion 10.6 we\n",
            "will discuss the phenomenon of diffraction which is based on Huygens-\n",
            "Fresnel principle. Finally in Sec tion 10.7 we will discuss the\n",
            "phenomenon of polari sation which is based on the fact that the light from 0 to 0.01639344262295082 based on rank 1 in query '1. Huygens principle explanation'\n",
            "Updating score for in Chapter 9, can also be understood on the basis of W ave Optics. Then\n",
            "we described Y oung’s double slit experiment which was a tur ning point\n",
            "in the study of optics. Finally, we described some associated points such\n",
            "as diffraction, resolution, polarisation, and validity of ray optics. In the\n",
            "next chapter , you will see how new experiments led to new theories at\n",
            "the turn of the century around 1900 A.D.\n",
            "SUMMARY\n",
            "1.Huygens’ principle tells us that each point on a wavefront is a source from 0 to 0.016129032258064516 based on rank 2 in query '1. Huygens principle explanation'\n",
            "Updating score for consistent with experiment?\n",
            "10.13 You have lear nt in the text how Huygens’ principle leads to the laws\n",
            "of reflection and refraction. Use the same principle to deduce directly\n",
            "that a point object placed in front of a plane mirror produces a\n",
            "virtual image whose distance from the mirror is equal to the object\n",
            "distance fr om the mirr or.\n",
            "10.14 Let us list some of the factors, which could possibly influence the\n",
            "speed of wave propagation:\n",
            "(i)nature of the source.\n",
            "(ii)direction of propagation. from 0 to 0.015873015873015872 based on rank 3 in query '1. Huygens principle explanation'\n",
            "Updating score for In this chapter we will first discuss the original formulation of the\n",
            "Huygens principle  and derive the laws of reflection and refraction. In\n",
            "Sections 10.4 and 10.5 , we will discuss the phenomenon of interference\n",
            "which is based on the principle of superposition. In Sec tion 10.6 we\n",
            "will discuss the phenomenon of diffraction which is based on Huygens-\n",
            "Fresnel principle. Finally in Sec tion 10.7 we will discuss the\n",
            "phenomenon of polari sation which is based on the fact that the light from 0.01639344262295082 to 0.03306010928961749 based on rank 0 in query '2. Applications of Huygens principle'\n",
            "Updating score for Physics\n",
            "354small portion of the sphere can be considered as a plane and we have\n",
            "what is known as a plane wave  [Fig. 10.1(b)].\n",
            "Now, if we know the shape of the wavefront at t = 0, then Huygens\n",
            "principle allows us to determine the shape of the wavefront at a later\n",
            "time τ. Thus, Huygens principle is essentially a geometrical construction,\n",
            "which given the shape of the wafefront at any time allows us to determine\n",
            "the shape of the wavefront at a later time. Let us consider a diverging from 0.016666666666666666 to 0.03306010928961749 based on rank 1 in query '2. Applications of Huygens principle'\n",
            "Updating score for consistent with experiment?\n",
            "10.13 You have lear nt in the text how Huygens’ principle leads to the laws\n",
            "of reflection and refraction. Use the same principle to deduce directly\n",
            "that a point object placed in front of a plane mirror produces a\n",
            "virtual image whose distance from the mirror is equal to the object\n",
            "distance fr om the mirr or.\n",
            "10.14 Let us list some of the factors, which could possibly influence the\n",
            "speed of wave propagation:\n",
            "(i)nature of the source.\n",
            "(ii)direction of propagation. from 0.015873015873015872 to 0.03200204813108039 based on rank 2 in query '2. Applications of Huygens principle'\n",
            "Updating score for in Chapter 9, can also be understood on the basis of W ave Optics. Then\n",
            "we described Y oung’s double slit experiment which was a tur ning point\n",
            "in the study of optics. Finally, we described some associated points such\n",
            "as diffraction, resolution, polarisation, and validity of ray optics. In the\n",
            "next chapter , you will see how new experiments led to new theories at\n",
            "the turn of the century around 1900 A.D.\n",
            "SUMMARY\n",
            "1.Huygens’ principle tells us that each point on a wavefront is a source from 0.016129032258064516 to 0.03200204813108039 based on rank 3 in query '2. Applications of Huygens principle'\n",
            "Updating score for Physics\n",
            "354small portion of the sphere can be considered as a plane and we have\n",
            "what is known as a plane wave  [Fig. 10.1(b)].\n",
            "Now, if we know the shape of the wavefront at t = 0, then Huygens\n",
            "principle allows us to determine the shape of the wavefront at a later\n",
            "time τ. Thus, Huygens principle is essentially a geometrical construction,\n",
            "which given the shape of the wafefront at any time allows us to determine\n",
            "the shape of the wavefront at a later time. Let us consider a diverging from 0.03306010928961749 to 0.04972677595628415 based on rank 0 in query '3. Wave propagation according to Huygens principle'\n",
            "Updating score for consistent with experiment?\n",
            "10.13 You have lear nt in the text how Huygens’ principle leads to the laws\n",
            "of reflection and refraction. Use the same principle to deduce directly\n",
            "that a point object placed in front of a plane mirror produces a\n",
            "virtual image whose distance from the mirror is equal to the object\n",
            "distance fr om the mirr or.\n",
            "10.14 Let us list some of the factors, which could possibly influence the\n",
            "speed of wave propagation:\n",
            "(i)nature of the source.\n",
            "(ii)direction of propagation. from 0.03200204813108039 to 0.04839549075403121 based on rank 1 in query '3. Wave propagation according to Huygens principle'\n",
            "Updating score for 355Wave Optics\n",
            "10.3 REFRACTION  AND REFLECTION  OF\n",
            "PLANE WAVES USING  HUYGENS\n",
            "PRINCIPLE\n",
            "10.3.1  Refraction of a plane wave\n",
            "We will now use Huygens principle to derive the laws of\n",
            "refraction. Let PP ′ represent the surface separating medium\n",
            "1 and medium 2, as shown in Fig. 10.4. Let v1 and v2\n",
            "represent the speed of light in medium 1 and medium 2,\n",
            "respectively. W e assume a plane wavefr ont AB pr opagating\n",
            "in the direction A ′A incident on the interface at an angle i from 0 to 0.016129032258064516 based on rank 2 in query '3. Wave propagation according to Huygens principle'\n",
            "Updating score for 353Wave Optics\n",
            "10.2  H UYGENS  PRINCIPLE\n",
            "We would first define a wavefr ont: when we dr op a small stone on a calm\n",
            "pool of water , waves spr ead out fr om the point of impact. Every point on\n",
            "the surface starts oscillating with time. At any instant, a photograph of\n",
            "the surface would show circular rings on which the disturbance is\n",
            "maximum. Clearly, all points on such a circle are oscillating in phase\n",
            "because they are at the same distance from the source. Such a locus of from 0 to 0.015873015873015872 based on rank 3 in query '3. Wave propagation according to Huygens principle'\n",
            "Updating score for consistent with experiment?\n",
            "10.13 You have lear nt in the text how Huygens’ principle leads to the laws\n",
            "of reflection and refraction. Use the same principle to deduce directly\n",
            "that a point object placed in front of a plane mirror produces a\n",
            "virtual image whose distance from the mirror is equal to the object\n",
            "distance fr om the mirr or.\n",
            "10.14 Let us list some of the factors, which could possibly influence the\n",
            "speed of wave propagation:\n",
            "(i)nature of the source.\n",
            "(ii)direction of propagation. from 0.04839549075403121 to 0.06506215742069787 based on rank 0 in query '4. Huygens-Fresnel principle comparison'\n",
            "Updating score for In this chapter we will first discuss the original formulation of the\n",
            "Huygens principle  and derive the laws of reflection and refraction. In\n",
            "Sections 10.4 and 10.5 , we will discuss the phenomenon of interference\n",
            "which is based on the principle of superposition. In Sec tion 10.6 we\n",
            "will discuss the phenomenon of diffraction which is based on Huygens-\n",
            "Fresnel principle. Finally in Sec tion 10.7 we will discuss the\n",
            "phenomenon of polari sation which is based on the fact that the light from 0.03306010928961749 to 0.04945355191256831 based on rank 1 in query '4. Huygens-Fresnel principle comparison'\n",
            "Updating score for in Chapter 9, can also be understood on the basis of W ave Optics. Then\n",
            "we described Y oung’s double slit experiment which was a tur ning point\n",
            "in the study of optics. Finally, we described some associated points such\n",
            "as diffraction, resolution, polarisation, and validity of ray optics. In the\n",
            "next chapter , you will see how new experiments led to new theories at\n",
            "the turn of the century around 1900 A.D.\n",
            "SUMMARY\n",
            "1.Huygens’ principle tells us that each point on a wavefront is a source from 0.03200204813108039 to 0.048131080389144903 based on rank 2 in query '4. Huygens-Fresnel principle comparison'\n",
            "Updating score for 355Wave Optics\n",
            "10.3 REFRACTION  AND REFLECTION  OF\n",
            "PLANE WAVES USING  HUYGENS\n",
            "PRINCIPLE\n",
            "10.3.1  Refraction of a plane wave\n",
            "We will now use Huygens principle to derive the laws of\n",
            "refraction. Let PP ′ represent the surface separating medium\n",
            "1 and medium 2, as shown in Fig. 10.4. Let v1 and v2\n",
            "represent the speed of light in medium 1 and medium 2,\n",
            "respectively. W e assume a plane wavefr ont AB pr opagating\n",
            "in the direction A ′A incident on the interface at an angle i from 0.016129032258064516 to 0.03200204813108039 based on rank 3 in query '4. Huygens-Fresnel principle comparison'\n",
            "Final reranked results: {'consistent with experiment?\\n10.13 You have lear nt in the text how Huygens’ principle leads to the laws\\nof reflection and refraction. Use the same principle to deduce directly\\nthat a point object placed in front of a plane mirror produces a\\nvirtual image whose distance from the mirror is equal to the object\\ndistance fr om the mirr or.\\n10.14 Let us list some of the factors, which could possibly influence the\\nspeed of wave propagation:\\n(i)nature of the source.\\n(ii)direction of propagation.': 0.06506215742069787, 'Physics\\n354small portion of the sphere can be considered as a plane and we have\\nwhat is known as a plane wave  [Fig. 10.1(b)].\\nNow, if we know the shape of the wavefront at t = 0, then Huygens\\nprinciple allows us to determine the shape of the wavefront at a later\\ntime τ. Thus, Huygens principle is essentially a geometrical construction,\\nwhich given the shape of the wafefront at any time allows us to determine\\nthe shape of the wavefront at a later time. Let us consider a diverging': 0.04972677595628415, 'In this chapter we will first discuss the original formulation of the\\nHuygens principle  and derive the laws of reflection and refraction. In\\nSections 10.4 and 10.5 , we will discuss the phenomenon of interference\\nwhich is based on the principle of superposition. In Sec tion 10.6 we\\nwill discuss the phenomenon of diffraction which is based on Huygens-\\nFresnel principle. Finally in Sec tion 10.7 we will discuss the\\nphenomenon of polari sation which is based on the fact that the light': 0.04945355191256831, 'in Chapter 9, can also be understood on the basis of W ave Optics. Then\\nwe described Y oung’s double slit experiment which was a tur ning point\\nin the study of optics. Finally, we described some associated points such\\nas diffraction, resolution, polarisation, and validity of ray optics. In the\\nnext chapter , you will see how new experiments led to new theories at\\nthe turn of the century around 1900 A.D.\\nSUMMARY\\n1.Huygens’ principle tells us that each point on a wavefront is a source': 0.048131080389144903, '355Wave Optics\\n10.3 REFRACTION  AND REFLECTION  OF\\nPLANE WAVES USING  HUYGENS\\nPRINCIPLE\\n10.3.1  Refraction of a plane wave\\nWe will now use Huygens principle to derive the laws of\\nrefraction. Let PP ′ represent the surface separating medium\\n1 and medium 2, as shown in Fig. 10.4. Let v1 and v2\\nrepresent the speed of light in medium 1 and medium 2,\\nrespectively. W e assume a plane wavefr ont AB pr opagating\\nin the direction A ′A incident on the interface at an angle i': 0.03200204813108039, '353Wave Optics\\n10.2  H UYGENS  PRINCIPLE\\nWe would first define a wavefr ont: when we dr op a small stone on a calm\\npool of water , waves spr ead out fr om the point of impact. Every point on\\nthe surface starts oscillating with time. At any instant, a photograph of\\nthe surface would show circular rings on which the disturbance is\\nmaximum. Clearly, all points on such a circle are oscillating in phase\\nbecause they are at the same distance from the source. Such a locus of': 0.015873015873015872}\n"
          ]
        }
      ],
      "source": [
        "# Documents reranked accordig to RRF.\n",
        "reranked_results = reciprocal_rank_fusion(all_results)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "id": "6o8KPAIaEmVo"
      },
      "outputs": [],
      "source": [
        "final_output = generate_output(original_query, reranked_results)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "id": "gJxHR_mwF_CT",
        "outputId": "62d52199-ccee-47a4-c584-d45ffa638d2a"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"Huygens' principle is a fundamental concept in wave optics that states that each point on a wavefront can be considered as a source of secondary spherical wavelets. These wavelets combine to form the new wavefront at a later time. This principle allows us to explain phenomena like reflection, refraction, interference, diffraction, and polarization of light waves.\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 23
        }
      ],
      "source": [
        "final_output"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}